package com.bjpowernode.learn;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author admin
 */
public class ScheduleTest {

    public static void main(String[] args) {
        //获取对象
        ScheduledExecutorService s = Executors.newScheduledThreadPool(1);
        //延时2秒开始执行任务，间隔3秒执行一次
        s.scheduleAtFixedRate(()-> {
            System.out.println(System.currentTimeMillis());
            try{
                TimeUnit.SECONDS.sleep(6);
            }catch (Exception e){
                e.printStackTrace();
            }
        },2,3, TimeUnit.SECONDS);
    }

    public static void schedule(){
        ScheduledExecutorService s = Executors.newScheduledThreadPool(1);
        s.schedule(()->System.out.println(Thread.currentThread().getName()),2, TimeUnit.SECONDS);
        s.shutdown();
    }

}
